/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ifips5.nal.epizza.web;

import com.ifips5.nal.epizza.util.JDBCManager;
import com.ifips5.nal.epizza.web.model.Client;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.sql.rowset.WebRowSet;

/**
 *
 * @author Moi
 */
public class ClientManager 
{
    private BeanManager beanManager = null;
    private Client currentClient = null;
    private List<Client> clientList = new LinkedList<Client>();
    private Map<Integer, Client> clientMap = new HashMap<Integer, Client>();
    
    
    
    public ClientManager()
    {
    }
    
    public ClientManager(BeanManager bm)
    {
        this.beanManager = bm;
    }

    
    public void loadClients()
    {
        String command = "SELECT * FROM client";
       int[] keyCol = {1};

        WebRowSet wrs =  JDBCManager.getInstance().getRowSet(command, keyCol);
        try
        {
            while(wrs.next())
            {
                Client currentClient = new Client(wrs.getInt(1), wrs.getString(2), wrs.getString(3), wrs.getDouble(4),wrs.getString(5),wrs.getString(6));
                this.clientMap.put(wrs.getInt(1), currentClient);
                this.clientList.add(currentClient);
            }
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }        
    }
    
    public int insertClient(String name, String pwd, String addr, String tel)
    {
        int idInsertClient = -1;
        try
        {
            Connection connection = JDBCManager.getInstance().getConnection();
            Statement stmt = connection.createStatement();
            String command = "INSERT INTO client(name, address, points, tel, pwd) VALUES(\""+name+"\", \""+addr+"\", 0, \""+tel+"\", \""+pwd+"\") ";
            
            stmt.executeUpdate(command, stmt.RETURN_GENERATED_KEYS);
            ResultSet cle = stmt.getGeneratedKeys();
            if(cle.next())
            {
                idInsertClient = cle.getInt(1);
            }
            stmt.executeBatch();   
            connection.commit();
            connection.close();    
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }        
        return idInsertClient;
    }
    
    public Client logClient(String login, String pwd)
    {
        String command = "SELECT * FROM client WHERE client.name = \""+login+"\" AND client.pwd = \""+pwd+"\" ";
        int[] keyCol = {1};

        WebRowSet wrs =  JDBCManager.getInstance().getRowSet(command, keyCol);
        try
        {
            if(wrs.next())
            {
                return new Client(wrs.getInt(1), wrs.getString(2), wrs.getString(3), wrs.getDouble(4),wrs.getString(5),wrs.getString(6));
            }
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }        
        return null;
    }
    
    
    
    public BeanManager getBeanManager()
    {
        return beanManager;
    }

    public void setBeanManager(BeanManager beanManager)
    {
        this.beanManager = beanManager;
    }

    public Client getCurrentClient()
    {
        return currentClient;
    }

    public void setCurrentClient(Client currentClient)
    {
        this.currentClient = currentClient;
    }

    public List<Client> getClientList()
    {
        return clientList;
    }

    public void setClientList(List<Client> clientList)
    {
        this.clientList = clientList;
    }

    public Map<Integer, Client> getClientMap()
    {
        return clientMap;
    }

    public void setClientMap(Map<Integer, Client> clientMap)
    {
        this.clientMap = clientMap;
    }


    
    
    
}
